package modulo1.cci;

import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modulo1.cdp.Aluno;
import modulo1.cdp.Matricula;
import modulo1.cdp.Turma;
import modulo1.cgt.CadastroInternoApl;
import modulo1.cgt.ControleTurmaMatriculaApl;

@WebServlet(name = "RelatorioCtrl", urlPatterns = {"/Relatorio"})
public class RelatorioCtrl extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher cih = request.getRequestDispatcher("modulo1/rel/relatorio_cadastro.jsp");
        cih.forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String evento = request.getParameter("evento");
        if (evento != null) {
            if (evento.equals("resultado_aluno")) {
                try {
                    Aluno aluno = CadastroInternoApl.getInstance().alunoConsultar(Long.parseLong(request.getParameter("id_aluno")));
                    List<Matricula> matriculas = ControleTurmaMatriculaApl.getInstance().matriculaConsultar(aluno);
                    request.setAttribute("aluno", aluno);
                    request.setAttribute("matriculas", matriculas);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (evento.equals("turmas")) {
                try {
                    List<Turma> turmasEncerradas = ControleTurmaMatriculaApl.getInstance().turmaBuscarEncerrada();
                    List<Turma> turmasAbertas    = ControleTurmaMatriculaApl.getInstance().turmaBuscarAberta();
                    List<Turma> turmasLiberadas = ControleTurmaMatriculaApl.getInstance().turmaBuscarLiberada();
                    
                    request.setAttribute("turmas_encerradas", turmasEncerradas);
                    request.setAttribute("turmas_abertas", turmasAbertas);
                    request.setAttribute("turmas_liberadas", turmasLiberadas);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (evento.equals("resultado_turma_encerrada")) {
                try {
                    Turma turma = ControleTurmaMatriculaApl.getInstance().turmaConsultar(Long.parseLong(request.getParameter("id_turma")));
                    request.setAttribute("turma", turma);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (evento.equals("resultado_turma_aberta")) {
                try {
                    Turma turma = ControleTurmaMatriculaApl.getInstance().turmaConsultar(Long.parseLong(request.getParameter("id_turma")));
                    request.setAttribute("turma", turma);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (evento.equals("resultado_turma_liberada")) {
                try {
                    Turma turma = ControleTurmaMatriculaApl.getInstance().turmaConsultar(Long.parseLong(request.getParameter("id_turma")));
                    request.setAttribute("turma", turma);
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            RequestDispatcher cih = request.getRequestDispatcher("modulo1/rel/relatorio_form.jsp");
            cih.forward(request, response);
            return;
        }
        String buscar = request.getParameter("buscar");

        try {
            List<Aluno> alunos;
            if (buscar != null) {
                alunos = CadastroInternoApl.getInstance().alunoBuscarNome(buscar);
            } else {
                alunos = CadastroInternoApl.getInstance().alunoBuscar();
            }
            request.setAttribute("alunos", alunos);
        } catch (Exception ex) {
            // TODO Exceção buscar curso
            ex.printStackTrace();
        }

        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
